package com.mirabelle.juice.controller;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.context.FacesContext;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.mirabelle.juice.beans.Paginator;
import com.mirabelle.juice.model.Comment;
import com.mirabelle.juice.model.Recipe;
import com.mirabelle.juice.model.Collection;
import com.mirabelle.juice.service.RecipeService;

@Controller("C_RECIPE")
@Scope("session")
public class CRecipe implements Serializable {
	private static final long serialVersionUID = 1L;
	private transient Logger log = LoggerFactory.getLogger(getClass());
	private String rootPath = null;

	private Recipe recipe = null;
	private String newComment = null;
	private Paginator<Comment> paginator = null;
	private Recipe editingRecipe = null;
	private List<Collection> collections = null;

	@Autowired
	@Qualifier("RecipeService")
	protected RecipeService recipeService;
	/*
	@Autowired
	@Qualifier("AromaService")
	protected AromaService aromaService;
	*/
	@Autowired
	@Qualifier("C_LOGGED_AUTHOR")
	private CPreferences authorPreferences;
	@Autowired
	@Qualifier("C_RECIPE_SEARCH")
	private CRecipeSearch recipeSearch;

	@PostConstruct
	public void init() {
		log.debug("Initialisation du controlleur C_RECIPE.");
		rootPath = FacesContext.getCurrentInstance().getExternalContext()
				.getRealPath("/");
		paginator = new Paginator<Comment>(new ArrayList<Comment>(), 4);
		log.debug("Le chemin de base est : " + rootPath);
	}
	
	public void export() {
		log.debug("Export demande pour la recette : " + recipe.getName());
	}
	
	public String edit() {
		editingRecipe = recipe;
		log.debug("Edition de la recette : " + editingRecipe.getName());
		return "recipe_edit";
	}
	
	public String editNew() {
		editingRecipe = new Recipe();
		log.debug("Edition d'une nouvelle recette: ");
		return "recipe_edit";
	}
	
	public String read() {
		log.debug("Lecture de la recette : " + recipe.getName());
		return "recipe";
	}
	
	public String delete() {
		log.debug("Suppression de la recette : " + recipe.getName());
		//delete
		recipe = null;
		return "recipe";
	}
	
	public String saveRecipe() {
		log.debug("Sauvegarde de la recette : " + editingRecipe.getName());
		log.debug("Mise à jour de la liste des recettes pour l'index : " + recipeSearch.getRecipes().indexOf(recipe));
		recipeSearch.getRecipes().set(recipeSearch.getRecipes().indexOf(recipe), editingRecipe);
		recipe = editingRecipe;
		return "recipe";
	}

	public void addComment() {
		if ((null != newComment) && (newComment.length() > 0)) {
			Comment comment = new Comment();
			comment.setCreated(new Date());
			comment.setAuthor(authorPreferences.getLoggedAuthor());
			comment.setContent(newComment);
			comment.setRecipe(recipe);
			recipe.getComments().add(comment);
			recipeService.saveRecipe(recipe);
			paginator.setList(recipe.getComments());
			paginator.getLast();
			newComment = "";
		}
	}
	
	public boolean isNext() {
		return paginator.hasNext();
	}
	
	public boolean isPrevious() {
		return paginator.hasPrevious();
	}
	
	public void getNext() {
		log.debug("appel de paginator.getNext index : " + paginator.getIndex());
		paginator.getNext();
		log.debug("paginator.getNext ok index : " + paginator.getIndex());
	}
	
	public void getPrevious() {
		log.debug("appel de paginator.getPrevious index : " + paginator.getIndex());
		paginator.getPrevious();
		log.debug("paginator.getPrevious ok index : " + paginator.getIndex());
	}

	public Recipe getRecipe() {
		return recipe;
	}

	public void setRecipe(Recipe recipe) {
		this.recipe = recipe;
		paginator.reSetList(recipe.getComments());
		log.debug("Liste selectionnée : " + recipe.getName() + " commentaires : " + recipe.getComments().size() + " paginator index : " + paginator.getIndex());
	}

	public Recipe getEditingRecipe() {
		return editingRecipe;
	}

	public void setEditingRecipe(Recipe editingRecipe) {
		this.editingRecipe = editingRecipe;
	}

	public String getNewComment() {
		return newComment;
	}

	public void setNewComment(String newComment) {
		log.debug("Nouveau commentaire : " + newComment);
		this.newComment = newComment;
	}

	public Paginator<Comment> getPaginator() {
		return paginator;
	}

	public boolean isOwner() {
		if (authorPreferences.getLoggedAuthor().getId() == recipe.getAuthor().getId()) {
			log.debug("L'utilisateur est l'auteur");
			return true;
		}
		log.debug("L'utilisateur n'est pas l'auteur");
		return false;
	}

	public List<Collection> getCollections() {
		if ((null == collections)||(collections.size() == 0)) {
			collections = authorPreferences.getLoggedAuthor().getCollections();
		}
		return collections;
	}

	public void setCollections(List<Collection> collections) {
		this.collections = collections;
	}
}
